package mpcc.mpcc.activity.use_assistant;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import mpcc.mpcc.activity.MPCCActivity;
import mpcc.mpcc.activity.R;
import mpcc.mpcc.activity.busi_presentation.NewsListActivity;
import mpcc.mpcc.activity.busi_presentation.NewsShowActivity;
import mpcc.mpcc.activity.busi_presentation.NewsListActivity.MyAdapter;
import mpcc.mpcc.activity.busi_presentation.NewsListActivity.ViewHolder;
import mpcc.mpcc.base.BaseActivity;

public class SearchActivity extends BaseActivity {
	private List<Map<String, Object>> mData;
	private Button search;
	private EditText searchText;
	private ListView helplist;
	private Button askButton;
	private Button returnBtn;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.helplist);
		search = (Button) findViewById(R.id.searchBtn);
		searchText = (EditText) findViewById(R.id.searchText);
		helplist = (ListView) findViewById(R.id.helplist);
		askButton = (Button) findViewById(R.id.askButton);
		returnBtn = (Button) findViewById(R.id.returnButton);
		generateData("");
		MyAdapter adapter = new MyAdapter(this);
		helplist.setAdapter(adapter);
		helplist.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long id) {
				String newsid = (String) mData.get(position).get("questionid");
				showInfo(newsid);
			}
		});
		addChangeActListener(askButton, this, AskQueActivity.class);
		addChangeActListener(returnBtn, this, MPCCActivity.class);
		search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String searchTextStr=searchText.getText().toString();
				generateData(searchTextStr);

			}

		});
	}

	public void generateData(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String requestStr = "26100001" +str+ sdf.format(new Date());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", "1");
		map.put("questionid", "1");
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("title", "2");
		map.put("questionid", "2");
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("title", "3");
		map.put("questionid", "3");
		list.add(map);
		setmData(list);
	}

	public void setmData(List<Map<String, Object>> mData) {
		this.mData = mData;
	}

	public void showInfo(String newsid) {

		ArrayList<String> arrClass = new ArrayList<String>();
		arrClass.add(newsid);
		Bundle bundle = new Bundle();
		bundle.putSerializable("questionidlist", arrClass);

		Intent intent = new Intent();
		intent.putExtras(bundle);

		intent.setClass(this, QuestionShowActivity.class);
		SearchActivity.this.startActivity(intent);
	}

	public final class ViewHolder {
		public TextView title;
	}

	public class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return mData.size();
		}

		public Object getItem(int arg0) {
			return null;
		}

		public long getItemId(int arg0) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.vlist, null);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.title.setText((String) mData.get(position).get("title"));

			return convertView;
		}
	}
}
